package com.sxbdjw.situation.plugins;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.gwsd.bean.GWMsgBean;
import com.gwsd.bean.GWType;
import com.sxbdjw.situation.activity.AudioCallActivity;
import com.sxbdjw.situation.activity.RtcActivity;
import com.sxbdjw.situation.core.GWSDKManager;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.JSONMessageCodec;
import io.flutter.plugin.common.StringCodec;

public class IntercomPlugin implements BasicMessageChannel.MessageHandler {

    private BasicMessageChannel channel;

    private String TAG = "IntercomPlugin";

    private String LogTag = "IntercomPlugin";
    private String CHANNEL_NAME = "com.sxbdjw.channel.intercom";
    private static IntercomPlugin intercomPlugin;

    public static IntercomPlugin INSTANCE(FlutterEngine context) {
        if (intercomPlugin == null) {
            synchronized (GWSDKManager.class) {
                if (intercomPlugin == null) {
                    intercomPlugin = new IntercomPlugin(context);
                }
            }
        }
        return intercomPlugin;
    }

    public static IntercomPlugin getPlugin() {
        return intercomPlugin;
    }

    private IntercomPlugin(FlutterEngine flutterEngine) {
        channel = new BasicMessageChannel<>(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL_NAME, JSONMessageCodec.INSTANCE);
        channel.setMessageHandler(this);
    }


    public void sendMsg(int event, String msg) {
        new Handler(Looper.getMainLooper()).post(() -> {
            if (channel != null) {
                channel.send(new JSONObject() {{
                    put("event", event);
                    put("msg", msg);
                }});
            } else {
                Log.d(LogTag, "channel is null");
            }
        });
    }

    @Override
    public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply reply) {

        Log.d(TAG, "onMessage: " + message.toString());
        JSONObject jsonObject = JSONObject.parseObject(message.toString());

        switch (jsonObject.getString("event")) {
            case "login":
                GWSDKManager.getSdkManager().login(jsonObject.getString("account"), jsonObject.getString("password"));
                break;
            case "speak":
                GWSDKManager.getSdkManager().startSpeak();
                break;
            case "stopSpeak":
                GWSDKManager.getSdkManager().stopSpeak();
                break;
            case "queryGroup":
                GWSDKManager.getSdkManager().sendGroup();
                break;
            case "sendTextMsg":
                GWMsgBean gwMsgBean = GWSDKManager.getSdkManager().createMsgBean(jsonObject.getInteger("convType"), jsonObject.getInteger("convId"), jsonObject.getString("convName"), GWType.GW_MSG_TYPE.GW_PTT_MSG_TYPE_TEXT);
                gwMsgBean.getData().setContent(jsonObject.getString("content"));
                GWSDKManager.getSdkManager().sendMsg(gwMsgBean);
                break;
            case "sendPhotoOrVideo":
                GWMsgBean gwMsgBean1 = GWSDKManager.getSdkManager().createMsgBean(jsonObject.getInteger("convType"), jsonObject.getInteger("convId"), jsonObject.getString("convName"), jsonObject.getInteger("msgType"));
                gwMsgBean1.getData().setContent(jsonObject.getString("content"));
                gwMsgBean1.getData().setUrl(jsonObject.getString("url"));
                gwMsgBean1.getData().setThumbUrl(jsonObject.getString("thumbUrl"));
                GWSDKManager.getSdkManager().sendMsg(gwMsgBean1);
                break;
            case "onBtnVoiceCall":
                GWSDKManager.getSdkManager().onBtnVoiceCall(jsonObject.getInteger("convId"), jsonObject.getString("convName"));
                break;
            case "onBtnVideoCall":
                GWSDKManager.getSdkManager().onBtnVideoCall(jsonObject.getInteger("convId"), jsonObject.getString("convName"));
                break;
            case "getLocation":
                GWSDKManager.getSdkManager().getLocation();
                break;
            case "bdjwCall":
                RtcActivity.startAct(GWSDKManager.getSdkManager().getContext(), jsonObject);
                break;
        }

    }
}
